package com.zeus.rpc;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.junit.Test;

import java.net.InetSocketAddress;

/**
 * RPC服务端
 * com.zeus.rpc
 * Created by zengxm on 2016/4/23 0023.
 */
public class TerminalServer {

	public static final String host="localhost";
	public static final int port=9090;

	@Test
	public void start() throws TTransportException {
		TProcessor processor = new TerminalService.Processor<TerminalService.Iface>(new TerminalServiceImpl());
		TNonblockingServerSocket tSocketTranport = new TNonblockingServerSocket(
				new InetSocketAddress(host, port));

		TNonblockingServer.Args targs = new TNonblockingServer.Args(
				tSocketTranport);

		targs.protocolFactory(new TCompactProtocol.Factory());
		targs.processor(processor);

		TServer server = new TNonblockingServer(targs);
		System.out.println("server begin ............"+host+":"+port);
		server.serve();
	}

	public class TerminalServiceImpl implements TerminalService.Iface{

		@Override
		public TerminalResponse action(TerminalRequest req) throws TException {
			return new TerminalResponse().setData("action");
		}

		@Override
		public TerminalResponse status(TerminalRequest req) throws TException {
			return new TerminalResponse().setData("running");
		}

		@Override
		public TerminalResponse shutdown(TerminalRequest req) throws TException {
			return new TerminalResponse().setSuccess(true);
		}
	}

	public void client(){

	}
}
